import {
    createSlice
} from "@reduxjs/toolkit";
import {useSelector} from "react-redux";
import {
    getHospitalDepartment,
    getHospitalList,
    getHospitalScheduleRule,
    getHospitalShow,
    getHospitalStatus
} from "@/api/hosp";
import {changeTitle} from "@/store/slice/common";
import {getFindByParentId} from "@/api/dict";


const initialState = {
    hospList:[],
    showList: {},
    pageInfo:{
        // current:(localStorage.current?(localStorage.current)/1:1),
        current:localStorage.getItem("current")?parseInt(localStorage.getItem("current") as string):1,
        pageSize:5,
        total:20
    },
    pageInfoRule:{
        current: 1,
        pageSize: 5,
        total: 1
    },
    params:{},
    departmentList:[],
    depcode:"",
    bookingScheduleList:[],
    provinceCodeList:[]
}
const hospListSlice = createSlice({
    name:"hospList",
    initialState,
    reducers:{
        changeParams(state,{payload}){
            state.params = payload
        },
        upHospList(state,{payload}){
            state.hospList = payload
        },
        changePageInfo(state,{payload}){
            state.pageInfo = payload
        },
        changePageInfoRule(state,{payload}){
            state.pageInfoRule = payload
        },
        upShowList(state,{payload}){
            console.log(payload)
            state.showList = payload
        },
        changDepartmentList(state,{payload}){
            state.departmentList = payload
        },
        changeDepcode(state,{payload}){
            state.depcode = payload
        },
        changeBookingScheduleList(state,{payload}){
            state.bookingScheduleList = payload
        },
        changeProvinceCodeList(state,{payload}){
            state.provinceCodeList = payload
        }
    }
})
export const {upHospList,changePageInfoRule,changeProvinceCodeList,changePageInfo,upShowList,changeParams,changeDepcode,changDepartmentList,changeBookingScheduleList} = hospListSlice.actions
//获取医院列表
export const getHospitalListAsync = function (page:number,limit:number){
    return async (dispatch:any,gatState:any)=>{
        const result =  await  getHospitalList(page,limit,gatState().hospList.params)
        console.log(result.data.content)
        localStorage.setItem("current",result.data.number+1)
        dispatch(upHospList(result.data.content))
        dispatch(changePageInfo({
            current:result.data.number+1,
            pageSize:result.data.size,
            total:result.data.totalElements
        }))
    }
}
// 获取分页列表
export const getHospitalStatusAsync = function (id:string,status:number){
        return async (dispatch:any,gatState:any)=>{
           const result =  await getHospitalStatus(id,status)
            const {current,pageSize} = gatState().hospList.pageInfo
            dispatch(getHospitalListAsync(current,pageSize))
        }
}

//获取查看详情
export const getHospitalShowAsync = (id:string,pathname:any)=>{
    return async (dispatch:any)=>{
        const result =  await getHospitalShow(id)
        // console.log(result.data)
            dispatch(upShowList(result.data))
            dispatch(changeTitle({
                key:pathname,
                title:result.data.hospital.hosname
            }))
    }
}

//排班列表
export const getHospitalDepartmentAsync = function (hoscode:any){
    return async (dispatch:any)=>{
        const result =  await getHospitalDepartment(hoscode)
        dispatch(changDepartmentList(result.data))
    }
}

//获取workDate排班的详细列表
export const getHospitalScheduleRuleAsync = (current:number,pageSize:number,hoscode:string,depcode:string)=>{
        return async (dispatch:any)=>{
            const {data} = await getHospitalScheduleRule(current,pageSize,hoscode as string,depcode)
            dispatch(changeBookingScheduleList(data.bookingScheduleList))
            dispatch(changePageInfoRule({
                current,
                pageSize,
                total:data.total
            }))
        }
}
//根据id获取数据列表
export const getFindEveryByIdAsync = function (parentId:number){
    return async (dispatch:any)=>{
        const result = await getFindByParentId(parentId)
        // console.log(result.data)
        dispatch(changeProvinceCodeList(result.data))
    }
}

export const useSelectorHospList = ()=>useSelector((state:any) =>state.hospList )
export  default  hospListSlice.reducer